using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace Sio.Mdm.Utils
{
    /// <summary>
    /// Basic disk writer class.
    /// </summary>
    public class LoggerDiskWriter : ILoggerWriter
    {
        #region Construction
        /// <summary>
        /// Construct writer for writting messages to disk
        /// </summary>
        /// <param name="filePath"></param>
        public LoggerDiskWriter(string filePath)
        {
            _filePath = filePath;
        }
        #endregion

        #region Properties

        #region Public

        #region FilePath
        private string _filePath;
        /// <summary>
        /// Get file path this writer is using
        /// </summary>
        public string FilePath
        {
            get
            {
                return _filePath;
            }
            set
            {
                _filePath = value;
            }
        }
        #endregion

        #region AppendDate
        private bool _appendDate = true;
        /// <summary>
        /// Get or set wheater date should be added to file name
        /// </summary>
        public bool AppendDate
        {
            get
            {
                return _appendDate;
            }
            set
            {
                _appendDate = value;                
            }
        }
        #endregion

        #endregion

        #endregion

        #region Methods

        #region Private

        #region GetFileName
        private string GetFileName()
        {
            if (AppendDate)
            {                
                string path = _filePath;
             
                if (!Path.IsPathRooted(path))
                {
                    path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, path);
                }

                string extension = Path.GetExtension(_filePath);
                string file = Path.GetFileNameWithoutExtension(_filePath);
                path = Path.GetDirectoryName(path);

                return Path.Combine
                    (
                    path, 
                    String.Format("{0}_{1}{2}",
                    file, DateTime.Today.ToString("ddMMyyyy"), extension)
                    );
            }
            else
            {
                return _filePath;
            }
        }          
        #endregion

        #endregion

        #endregion

        #region ILoggerWriter Members
        void ILoggerWriter.WriteMessage(Logger.MessageType type, DateTime timeStamp, string message)
        {
            try
            {
                if (!String.IsNullOrEmpty(_filePath))
                {
                    using (FileStream stream = new FileStream(GetFileName(), FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite))
                    {
                        stream.Seek(0, SeekOrigin.End);

                        using (StreamWriter writer = new StreamWriter(stream))
                        {   
                            writer.WriteLine(String.Format("[{1}] {0} {2}", timeStamp, type, message));
                        }

                        stream.Close();
                    }
                }
            }
            catch (Exception ex)
            {
                ex.ToString();
            }
        }
        #endregion
    }
}
